<template>
  <v-layout type="main">

    <div class="layout-top">
      <div class="rollback" @click="$router.push('/image')">
        <img src="~@/assets/img/prev.png" alt="">
        <span>返回</span>
      </div>

      <div class="code-description">
        <div class="dea_top">
<!--          <div>-->
<!--            <el-image class="nav_box" :src="detailData.avatar" fit="fit"></el-image>-->
<!--          </div>-->

          <div class="header_box">
            <div class="code-desc-header">
              <div class="title">
                <div class="title-header">
                  <span>{{ detailData.name }}</span>
                </div>

                <div class="code-tags">
                  <div class="user-tag tag">
                    <img src="~@/assets/img/user.png" alt="">
                    {{
                      detailData.create_user && (detailData.create_user.nick_name ||
                        detailData.create_user.username)
                    }}
                  </div>

                  <div :class="['status-tag tag', {
                        'danger': detailData.status === 'terminated',
                        'warning': detailData.status != 'terminated' && detailData.status != 'running',
                        'success': detailData.status === 'running'
                      }]">
                    {{ statusMap[detailData.status] }}
                  </div>


                  <div class="time-tag tag">
                    <span>创建时间：</span>
                    {{ detailData.create_time }}
                  </div>

                </div>

                <div class="staet_box">
                  <plug-state :statusStr="detailData.status" />
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>


      <div class="btns-container">
        <div class="alick-handle-btns">
          <el-button class="handle-btn" type="success" size="medium" @click="startBuild"
            v-show="detailData.status == 'terminated' && detailData.build_type == '1'">
            构建镜像
          </el-button>
          <el-button class="handle-btn" type="danger" size="medium" @click="stopBuild"
            v-show="detailData.status != 'terminated'">
            停止构建
          </el-button>
        </div>
      </div>
    </div>


    <!-- <el-radio-group v-model="nowRouter" size="medium" @change="skip" class="nav-alike" v-show="!$route.meta.hideDetails">
      <el-radio-button :label="e.name" v-for="(e, i) in thisRouter" :key="i">
        <span class="nav-alike-item">{{ e.meta.title }}</span>
      </el-radio-button>
    </el-radio-group> -->

    <div class="tabs-left" v-if="thisRouter.length > 1">
      <el-tabs v-model="nowRouter" @tab-click="skip" v-show="!$route.meta.hideDetails">
        <el-tab-pane :label="e.meta.title" :name="e.name" v-for="(e, i) in thisRouter" :key="i" />
      </el-tabs>
    </div>

    <div :class="['layout-content', { 'layout-content-nodetail': $route.meta.hideDetails }]">
      <router-view class="content-detail" :detailData.sync="detailData" v-if="detailData" @getDetail="getDetail" />
    </div>

    <!-- <el-dialog :visible.sync="showUpdateBaseDialog" title="我的环境基本信息" width="840px">
      <el-form class="form myself-form" ref="form" :rules="submitRules" :model="formData" label-width="120px">
        <el-form-item label="名称" prop="name">
          <el-input type="text" v-model="formData.name" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input type="textarea" :rows="3" v-model="formData.desc" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="formData.type" @change="changeEngine()">
            <el-radio :label="1">训练</el-radio>
            <el-radio :label="2">推理</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label=" ">
          <el-button class="none-type" type="primary" @click="commitBase">确定</el-button>
          <el-button class="none-type" @click="cancelBase">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog> -->
  </v-layout>
</template>

<script>
import plugState from '@/components/module/plug-state'
import { getImageDetail, buildImage, stopBuildImage } from '@/api/image'
export default {
  data() {
    return {
      nowRouter: this.$route.name,
      thisRouter: [],
      detailData: null,
      // formData: {},
      // showUpdateBaseDialog: false,
      // submitRules: {
      //   name: [
      //     { required: true, message: "该项为必填项" },
      //     { required: true, min: 1, max: 40, message: "长度在1到40个字符" },
      //   ],
      //   desc: [
      //     { required: true, message: "该项为必填项" },
      //     { required: true, min: 1, max: 200, message: "长度在1到200个字符" },
      //   ],
      // },
    };
  },
  methods: {
    skip() {
      let path = `/image/${this.thisRouter.filter(e => e.name == this.nowRouter)[0].path}`
      if (path == this.$route.path) return
      this.$router.push({
        path,
        query: {
          id: this.$route.query.id
        }
      })
    },
    async getDetail() {
      if (this.$route.query.id) {
        console.log('id', this.$route.query.id)
        let res = await getImageDetail({ id: this.$route.query.id })
        this.detailData = res.data
        this.formData = JSON.parse(JSON.stringify(this.detailData))
      }
    },
    // updateBase() {
    //   this.showUpdateBaseDialog = true
    // },

    // commitBase() {
    //   this.$refs.form.validate(async (valid) => {
    //     if (valid) {
    //       let res = await putImage(this.formData);
    //       if (res && res.data) {
    //         this.getDetail()
    //         this.showUpdateBaseDialog = false
    //       }
    //     }
    //   })
    // },
    // cancelBase() {
    //   this.showUpdateBaseDialog = false
    // },
    async startBuild() {
      await buildImage({ id: this.detailData.id })
    },

    async stopBuild() {
      await stopBuildImage({ id: this.detailData.id })
    },
    updateTask() { }
  },
  mounted() {
    let temp = this.$router.options.routes.filter(
      e => e.name == 'image' && e.children
    )
    this.thisRouter = temp[0].children.filter(e => !e.meta.hideDetails)
    this.getDetail()
    this.$EventBus.$on('imagebuild', e => {
      console.log(e)
      this.getDetail()
    })
  },
  components: {
    plugState,
  },
  destroyed() {
    this.$EventBus.$off('imagebuild')
  },
}
</script>
<style lang="less" scoped>
@import "~@/assets/css/detail-layout.less";
</style>
